//
//  Search.swift
//  Motion
//
//  Created by 悦月越悦 on 2021/3/7.
//

import SwiftUI
import SwiftUIX

struct Search: View {
    @State var isEditing: Bool = false
    @State var searchText: String = ""
    @ObservedObject var  searchModel = SearchCourseModel()
    @Binding var presentSearchPage : Bool
    
    var body: some View {
        VStack{
            //搜索顶栏
            SearchTopBar(presentSearchPage: $presentSearchPage)
            //搜索框
            HStack{
                SearchBar("搜索一下，你就知道", text: $searchText, isEditing: $isEditing)
                Button(action: {
                    if(isEditing){
                        print("isEditing")
                        searchModel.setSearchCText(searchText)
                    }
                }) {
                    Text("搜索")
                        .customFont(18, true)
                        .foregroundColor(Color("topicPurple"))
                }
            }.padding(.horizontal,12)
            
            SearchRecordBar(searchModel: searchModel)
                .padding(.horizontal,8)
            SearchRecord(keyWords: $searchModel.searchCTexts)
                .padding(.horizontal,8)
            Spacer()
        }.onAppear(perform: {
            //页面进入时，获取历史搜索记录
            searchModel.getSearchCTexts()
        })
    }
}

struct Search_Previews: PreviewProvider {
    static var previews: some View {
        Search(presentSearchPage: .constant(false))
    }
}

extension View{
    func customFont(_ size:CGFloat,_ underline:Bool)-> some View{
        self
            .font(.system(size: size, weight: underline ?.bold:.light, design: .default))
    }
}
